package customer.ejb.eaos;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import model.Excursion;
import model.ExcursionPersTP;
import model.ExcursionPredTP;
import model.Hotel;
import common.Filter;
import common.IPredicate;
import dtos.ExcursionDTO;
import dtos.ExcursionPersTPDTO;
import dtos.ExcursionPredTPDTO;
import dtos.HotelDTO;

@Stateless
public class ExcursionManagerEAO implements ExcursionManager{

	@PersistenceContext
    private EntityManager em;
	
	private String place;
	private int travelID;

	public ExcursionManagerEAO(){
		
	}
	
	@Override
	public Collection<ExcursionDTO> getExcursionList(String place) {
		this.place = place;
		

		@SuppressWarnings("unchecked")
		List<Excursion> qres = em.createQuery("SELECT x FROM Excursion x").getResultList();
		Collection<Excursion> fres = Filter.filter(qres, isOK);
		Collection<ExcursionDTO> res = new ArrayList<ExcursionDTO>(fres.size());
		
		
		for(Excursion e : fres)
		{
			res.add(e.getDTO());
		}
		
		return res;
		
	}
	
	IPredicate<Excursion> isOK = new IPredicate<Excursion>() {
		public boolean apply(Excursion e) {
	        
	        return e.isAtPlace(place);
	    }

	};

	@Override
	public List<ExcursionDTO> getExcursionListPersonalizedTP(int travelID) {
		this.travelID = travelID;
		

		@SuppressWarnings("unchecked")
		List<ExcursionPersTP> qres = em.createQuery("SELECT x FROM ExcursionPersTP x").getResultList();
		Collection<ExcursionPersTP> fres = Filter.filter(qres, isFromPersonalizedTP);
		Collection<ExcursionPersTPDTO> res = new ArrayList<ExcursionPersTPDTO>(fres.size());
		List<ExcursionDTO> res2 = new ArrayList<ExcursionDTO>(fres.size());
		
		
		for(ExcursionPersTP e : fres)
		{
			res.add(e.getDTO());
		}
		for (ExcursionPersTPDTO e : res){
			res2.add(e.getExcursion());
		}
		
		return res2;
	}
	
	IPredicate<ExcursionPersTP> isFromPersonalizedTP = new IPredicate<ExcursionPersTP>() {
		public boolean apply(ExcursionPersTP e) {
	        
	        return e.isInPersonalizedTP(travelID);
	    }

	};

	IPredicate<ExcursionPredTP> isFromPredefinedTP = new IPredicate<ExcursionPredTP>() {
		public boolean apply(ExcursionPredTP e) {
	        
	        return e.isInPredefinedTP(travelID);
	    }

	};

	
	@Override
	public List<ExcursionDTO> getExcursionListPredefinedTP(int id) {
		this.travelID = id;
		

		@SuppressWarnings("unchecked")
		List<ExcursionPredTP> qres = em.createQuery("SELECT x FROM ExcursionPredTP x").getResultList();
		Collection<ExcursionPredTP> fres = Filter.filter(qres, isFromPredefinedTP);
		Collection<ExcursionPredTPDTO> res = new ArrayList<ExcursionPredTPDTO>(fres.size());
		List<ExcursionDTO> res2 = new ArrayList<ExcursionDTO>(fres.size());
		
		
		for(ExcursionPredTP e : fres)
		{
			res.add(e.getDTO());
		}
		for (ExcursionPredTPDTO e : res){
			res2.add(e.getExcursion());
		}
		
		return res2;
	}
	
}
